package com.test.springCloud.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * 上传base64的图片流
* @author:LuoXingFa;
* @version:Date:2022年8月2日上午10:34:25
*/
public class Base64CodeMultiFileUtil implements MultipartFile{

	private final byte[] imageContent;
	private final String header;
	
	//带参的构造函数
	public Base64CodeMultiFileUtil(byte[] imageContent, String header) {
		super();
		this.imageContent = imageContent;
		this.header = header.split(";")[0];
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return System.currentTimeMillis()+Math.random()+"."+header.split("/")[1];
	}

	@Override
	public String getOriginalFilename() {
		// TODO Auto-generated method stub
		return System.currentTimeMillis()+Math.random()*10000+"."+header.split("/")[1];
	}

	@Override
	public String getContentType() {
		// TODO Auto-generated method stub
		return header.split(":")[1];
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return imageContent==null||imageContent.length==0;
	}

	@Override
	public long getSize() {
		// TODO Auto-generated method stub
		return imageContent.length;
	}

	@Override
	public byte[] getBytes() throws IOException {
		// TODO Auto-generated method stub
		return imageContent;
	}

	@Override
	public InputStream getInputStream() throws IOException {
		// TODO Auto-generated method stub
		return new ByteArrayInputStream(imageContent);
	}

	@Override
	public void transferTo(File dest) throws IOException, IllegalStateException {
		// TODO Auto-generated method stub
		new FileOutputStream(dest).write(imageContent);
	}

}
